stack
Picture a pile of plates. You put a new plate on top, and when you want one, you take it from the top. You never pull one out of the middle. A stack works exactly like that: you push an item onto the top, you pop the top item off, and the last item pushed is always the first one to come back. That rule is called last in, first out.
The whole point of a stack is that it gives you only that one end. You cannot ask for the third item down, and you cannot remove one from the middle. That sounds like a limit, and it is the reason to choose one: a stack in your code tells the reader, and the compiler, that nothing will ever reach into the middle.
Reach for a stack when the newest thing is the one you want back first:
/// @type {stack<int>}
/// @capacity 16
var history = [];/// @type says every item is an int, and /// @capacity says the most items the stack can hold. Leave the capacity off and you get 8. It can be any number; a stack has no rule about which sizes are allowed. Like every container on the GBA, its space is set aside when the game is built, not grown while it runs.
The same stack with no comment: var history = stack_create(16, 0); (an empty stack of whole numbers that holds 16) or var history: stack<int, 16> = [];. See stack_create. All three are the same stack, and a stack given no size by any of them holds 8.
A starting list in brackets fills the stack from the bottom up, so the last item in the brackets is the top: [1, 2, 3] starts with 3 on top.
| Function | What it does | It gives back |
|---|---|---|
stack_push(stack, value) | puts value on top | true if it fit, false if the stack was full |
stack_pop(stack) | removes the top item | that item |
stack_peek(stack) | looks at the top item without removing it | that item |
stack_empty(stack) | asks whether there is nothing in it | true or false |
stack_contains(stack, value) | asks whether value is anywhere in it, without taking anything off | true or false |
stack_length(stack) | counts the items | the count |
stack_clear(stack) | empties it | nothing |
stack_copy(stack) | makes a second stack just like it, with the same room | the new stack |
Push, pop and peek each take the same short time however many items the stack holds.
Asking an empty stack for its top item does not crash the game. stack_pop and stack_peek hand back a plain default value: 0 for a whole number, 0.0 for a fractional one, false, an empty piece of text, or nothing for an instance reference. Nothing is removed, because there was nothing to remove.
That default is an ordinary value. 0 could also be a real item you pushed, so the result alone cannot tell you whether the stack was empty. Ask first: stack_empty(history) is the way to know. A loop that empties a stack uses it as its condition:
/// @type {stack<int>}
var history = [1, 2, 3];
while (not stack_empty(history)) {
var top = stack_pop(history); // 3, then 2, then 1
}stack_push on a full stack adds nothing and returns false. It does not throw away the oldest item to make room, and it does not crash. For an undo history that means that once the stack is full, new moves stop being recorded until something is popped. Decide what your game wants: check the answer and clear the stack, or size the capacity to the longest history you will allow.
[i]: the top is all you can reachLists and arrays are read with [i]. A stack is not: history[0] is refused when the project is checked, with a message that a stack cannot be indexed. That is deliberate, for the reason above. Accessors covers this across all the containers. If you find you need to look at the middle of the pile, you wanted a list.
An undo history for a game where the player pushes a block:
/// @type {stack<int>}
/// @capacity 8
var moves = [];
stack_push(moves, 1); // the player moved right
stack_push(moves, 3); // then moved down
var next = stack_peek(moves); // 3 - look, but leave it there
var undone = stack_pop(moves); // 3 - now it is gone, 1 is on top
var left = stack_length(moves); // 1An undo step is usually more than one number: which way the player moved, and whether a block moved with them. A struct bundles those into one item. The stack has to be shown what a struct looks like once, so it starts with one real item that is popped straight back off:
/// @type {stack<struct Move>}
var undo = [ { dx: 1, dy: 0 } ];
var seed = stack_pop(undo); // now empty - the shape is learned
stack_push(undo, { dx: 0, dy: -1 });
var last = stack_pop(undo);
var moved_up = last.dy; // -1The struct rules, and why the first item is needed, are on the Structs in data containers page.
Reach for a stack when the newest item is the one you want first. If the oldest should come out first, that is a queue. If you need to search the items, go through them in order, or remove one from the middle, use a list. Data containers compares all of them side by side.